﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace GoogleLanguage.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Translator runat=server></{0}:Translator>")]
    public class Translator : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        public string TargetControlID
        {
            get
            {
                if (ViewState["TargetControlID"] == null)
                {
                    ViewState["TargetControlID"] = String.Empty;
                }
                return (string)ViewState["TargetControlID"];
            }
            set
            {
                ViewState["TargetControlID"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        public Language DestinationLanguage
        {
            get
            {
                if (ViewState["DestinationLanguage"] == null)
                {
                    ViewState["DestinationLanguage"] = LanguageMapping.DEFAULT_LANGUAGE;
                }
                return (Language)ViewState["DestinationLanguage"];
            }
            set
            {
                ViewState["DestinationLanguage"] = value;
            }
        }        

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!String.IsNullOrEmpty(TargetControlID))
            {
                Control targetControl = FindControlRecursive(Page.Master != null ? Page.Master.Controls : Page.Controls, TargetControlID);
                if (targetControl != null)
                {
                    string scriptKey = String.Format("Translator_{0}", this.ClientID);
                    if (!Page.ClientScript.IsStartupScriptRegistered(scriptKey))
                    {
                        StringBuilder script = new StringBuilder();

                        if (targetControl is Panel || targetControl is Label || targetControl is HyperLink || targetControl is LinkButton)
                        {
                            script.Append(String.Format("GoogleLanguageInitializeLiteral(\"{0}\", \"{1}\");", targetControl.ClientID, LanguageMapping.GetLanguageCode(DestinationLanguage)));
                        }
                        else if (targetControl is TextBox || targetControl is Button)
                        {
                            script.Append(String.Format("GoogleLanguageInitializeValue(\"{0}\", \"{1}\");", targetControl.ClientID, LanguageMapping.GetLanguageCode(DestinationLanguage)));
                        }
                        else if (targetControl is CheckBox)
                        {
                            script.Append(String.Format("GoogleLanguageInitializeSiblingValue(\"{0}\", \"{1}\");", targetControl.ClientID, LanguageMapping.GetLanguageCode(DestinationLanguage)));
                        }

                        Page.ClientScript.RegisterStartupScript(this.GetType(), scriptKey, script.ToString(), true);
                    }
                }
            }
        }

        private Control FindControlRecursive(ControlCollection controls, string controlID)
        {
            Control ctl = null;
            if (controls != null && controls.Count > 0)
            {
                if (ctl == null)
                {
                    foreach (Control c in controls)
                    {
                        if (c.ID == controlID)
                        {
                            ctl = c;
                            break;
                        }
                        ctl = FindControlRecursive(c.Controls, controlID);
                        if (ctl != null) break;
                    }
                }
            }
            return ctl;
        }
    }
}
